package com.hc.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.hc.api.enums.GoodsEnum;
import com.hc.api.enums.ImageStateEnum;
import com.hc.api.enums.VoucherStateEnum;
import com.hc.api.utils.ApiUtil;
import com.hc.comm.base.BaseController;
import com.hc.comm.dto.*;
import com.hc.comm.exception.OprationStatus;
import com.hc.comm.utils.CollectionUtils;
import com.hc.comm.utils.JsonUtils;
import com.hc.comm.utils.StringUtil;
import com.hc.comm.vo.HospitalVO;
import com.hc.comm.vo.ImageVO;
import com.hc.comm.vo.VoucherDataVO;
import com.hc.comm.vo.VoucherTypeVO;
import com.hc.service.model.*;
import com.hc.service.service.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * App首页
 * Created by yx on 2017/4/10.
 */
@Controller
@Scope("prototype")
@RequestMapping("/" + ApiUtil.ApiVersion.V1 + "/")
public class AppHomePageController extends BaseController {

    @Resource(name = "bannersService")
    private IBannersService bannersService;

    @Resource(name = "voucherTypeService")
    private IVoucherTypeService voucherTypeService;

    @Resource(name = "hospitalTypeService")
    private IHospitalTypeService hospitalTypeService;

    @Resource(name = "hospitalService")
    private IHospitalService hospitalService;

    @Resource(name = "voucherDataService")
    private IVoucherDataService voucherDataService;

    @Resource(name = "imageService")
    private IImageService imageService;

    @RequestMapping("banners")
    public void getBanners() throws Exception {
        //List<BannersModel> banners = bannersService.getShowBannerByState(1);
        BannersDTO bannersDTO = new BannersDTO();
        bannersDTO.setRows(6);
        bannersDTO.setPage(1);
        bannersDTO.setState(1);
        bannersDTO.setSort("showSort");
        List<BannersModel> banners = bannersService.getList(bannersDTO);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String[] filterParm = {"id", "typeName", "bannerTitle", "linkUrl", "imageUrl", "showSort"};
        resultMap.put("banners", JsonUtils.getFilterObject(banners, filterParm));
        JsonUtils.toSuccessResult(request, response, resultMap);
    }


    @RequestMapping("voucher_type_list")
    public void getVoucherTypeList() throws Exception {
        List<VoucherTypeModel> types = voucherTypeService.getList(new VoucherTypeDTO());
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("types", JsonUtils.getFilterObject(types, "id", "typeName", "typeIcon"));
        JsonUtils.toSuccessResult(request, response, resultMap);

    }

    /**
     * 获取推荐优惠券
     *
     * @param request
     * @throws Exception
     */
    @RequestMapping("voucher_list")
    public void getVoucherList(HttpServletRequest request) throws Exception {
        //优惠券类型，为空时获取全部类型
        String voucher_type_id = request.getParameter("voucher_type_id");
        //医院类型，为空时获取全部类型
        String hospital_type_id = request.getParameter("hospital_type_id");
        //医院，为空时获取全部类型
        String hospital_id = request.getParameter("hospital_id");

        String page = request.getParameter("page");

        if ((!StringUtil.isEmpty(voucher_type_id) && !StringUtil.isNumeric(voucher_type_id))
                || (!StringUtil.isEmpty(hospital_type_id) && !StringUtil.isNumeric(hospital_type_id))
                || (!StringUtil.isEmpty(hospital_id) && !StringUtil.isNumeric(hospital_id))
                || (!StringUtil.isEmpty(page) && !StringUtil.isNumeric(page))) {
            //writeToJson("参数有误！");
            JsonUtils.toErrorResult(request, response, "参数有误!", OprationStatus.FAIL.getCode());
        } else {
            VoucherDataDTO dataDTO = new VoucherDataDTO();
            if (!StringUtil.isEmpty(voucher_type_id) && Integer.valueOf(voucher_type_id) > 0) {
                dataDTO.setVoucherType(Integer.valueOf(voucher_type_id));
            }
            if (!StringUtil.isEmpty(hospital_id) && Integer.valueOf(hospital_id) > 0) {
                dataDTO.setHospitalId(Integer.valueOf(hospital_id));
            }
            dataDTO.setVoucherState(VoucherStateEnum.RECOMMEND.getCode());
            dataDTO.setRows(20);
            dataDTO.setPage(page == null ? 1 : Integer.valueOf(page));
            //获取到传的医院id的和优惠券类型的优惠券
            List<VoucherDataModel> voucherModels = voucherDataService.getList(dataDTO);
            if (!StringUtil.isEmpty(hospital_type_id)) {
                List hospitalIdList = CollectionUtils.getBeanFieldValues(voucherModels, "hospitalId");
                HospitalDTO hospitalDTO = new HospitalDTO();
                hospitalDTO.setIdList(hospitalIdList);
                hospitalDTO.setTypeId(Integer.valueOf(hospital_type_id));
                List<HospitalModel> hospitalList = hospitalService.getList(hospitalDTO);
                Map<Long, HospitalModel> hospitalMap = CollectionUtils.toMap(hospitalList, "id");
                List<VoucherDataVO> voList = new ArrayList<>();
                for (VoucherDataModel datamodel : voucherModels) {
                    if (hospitalMap.containsKey(datamodel.getHospitalId())) {
                        VoucherDataVO dataVo = new VoucherDataVO();
                        dataVo.copyPropertis(datamodel);
                        voList.add(dataVo);
                    }
                }
                JsonUtils.toSuccessResult(request, response, voList, "id", "voucherName", "voucherType", "voucherDescription",
                        "voucherState", "voucherMoney", "voucherIcon", "voucherState", "startTime", "endTime");
            } else {
                JsonUtils.toSuccessResult(request, response, voucherModels, "id", "voucherName", "voucherType", "voucherDescription",
                        "voucherState", "voucherMoney", "voucherIcon", "voucherState", "startTime", "endTime");
            }

        }
    }

    /**
     * 获取优惠券详细信息
     *
     * @param request
     * @throws Exception
     */
    @RequestMapping("voucher")
    public void getVoucher(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //优惠券id
        String voucher_id = request.getParameter("voucher_id");

        if (StringUtil.isEmpty(voucher_id) || !StringUtil.isNumeric(voucher_id)) {
            JsonUtils.toErrorResult(request, response, "参数有误!", OprationStatus.FAIL.getCode());
        } else {
            VoucherDataDTO voucherDataDTO = new VoucherDataDTO();
            voucherDataDTO.setId(Integer.valueOf(voucher_id));
            VoucherDataModel voucherData = voucherDataService.getOne(voucherDataDTO);
            VoucherDataVO voucherDataVO = new VoucherDataVO();
            voucherDataVO.copyPropertis(voucherData);

            HospitalDTO hospitalDTO = new HospitalDTO();
            hospitalDTO.setId(voucherDataVO.getHospitalId());
            HospitalModel hospitalModel = hospitalService.getOne(hospitalDTO);
            HospitalVO hospitalVO = new HospitalVO();
            hospitalVO.copyPropertis(hospitalModel);
            voucherDataVO.setHospitalVO(hospitalVO);

            VoucherTypeDTO voucherTypeDTO = new VoucherTypeDTO();
            voucherTypeDTO.setId(voucherData.getVoucherType());
            VoucherTypeModel voucherTypeModel = voucherTypeService.getOne(voucherTypeDTO);
            VoucherTypeVO voucherTypeVO = new VoucherTypeVO();
            voucherTypeVO.copyPropertis(voucherTypeModel);
            voucherDataVO.setVoucherTypeVO(voucherTypeVO);

            String[] parame = {"id", "voucherName", "voucherType", "voucherDescription",
                    "voucherState", "voucherMoney", "voucherIcon", "voucherState", "hospitalVO", "voucherTypeVO", "startTime", "endTime"};
            JsonUtils.toSuccessResult(request, response, voucherDataVO, parame);
        }
    }

    @RequestMapping("hospital_type_list")
    public void getHospitalTypeList() throws Exception {
        List<HospitalTypeModel> types = hospitalTypeService.getList(new HospitalTypeDTO());
        JsonUtils.toSuccessResult(request, response, types, "id", "typeName");
//        List<HospitalTypeModel> types = hospitalTypeService.getList(new HospitalTypeDTO());
//        SimplePropertyPreFilter filter = new SimplePropertyPreFilter(HospitalTypeModel.class, "id", "typeName");
//        writeToJson(types, filter);
    }

    /**
     * 获取医院列表
     *
     * @param request
     * @throws Exception
     */
    @RequestMapping("hospitals")
    public void getHospitalList(HttpServletRequest request) throws Exception {
        String page = request.getParameter("page");
        String hospital_type_id = request.getParameter("hospital_type_id");
        HospitalDTO hospitalDto = new HospitalDTO();
        if (StringUtil.isNum(page) && Integer.valueOf(page) > -1) {
            hospitalDto.setPage(Integer.valueOf(page));
        }
        if (StringUtil.isNum(hospital_type_id) && Integer.valueOf(hospital_type_id) > 0) {
            hospitalDto.setTypeId(Integer.valueOf(hospital_type_id));
        }
        hospitalDto.setRows(20);
        List<HospitalModel> hospitalList = hospitalService.getList(hospitalDto);
        JsonUtils.toSuccessResult(request, response, hospitalList,
                "id", "hospitalName", "hospitalDescription", "hospitalAdress", "typeId", "imageUrl");
    }

    /**
     * 获取某类型医院列表
     *
     * @param request
     * @throws Exception
     */
    @RequestMapping("hospital")
    public void getHospital(HttpServletRequest request) throws Exception {
        String hospital_id = request.getParameter("hospital_id");
        String page = request.getParameter("page");
        HospitalDTO hospitalDto = new HospitalDTO();

        if (StringUtil.isNum(hospital_id) && Integer.valueOf(hospital_id) > 0) {
            hospitalDto.setTypeId(Integer.valueOf(hospital_id));
        }
        if (StringUtil.isEmpty(page) || !StringUtil.isNumeric(page)) {
            hospitalDto.setPage(1);
        }
        hospitalDto.setRows(20);
        List listHospital = hospitalService.getList(hospitalDto);
        JsonUtils.toSuccessResult(request, response, listHospital,
                "id", "hospitalName", "hospitalDescription", "hospitalAdress", "typeId", "imageUrl");
    }

    /**
     * 获取医院详细信息
     *
     * @throws Exception
     */
    @RequestMapping("hospital/detail/{hospitalId}")
    public void getHospitalDetail(@PathVariable("hospitalId") int hospitalId) throws Exception {
        HospitalDTO hospitalDTO = new HospitalDTO();
        JSONObject result = new JSONObject();
        hospitalDTO.setId(hospitalId);
        try {
            HospitalModel hospitalModel = hospitalService.getOne(hospitalDTO);
            if (hospitalModel == null) {
                JsonUtils.toErrorResult(request, response, "参数有误!", OprationStatus.FAIL.getCode());
            } else {
                HospitalVO hospitalVO = new HospitalVO();
                hospitalVO.copyPropertis(hospitalModel);
                ImageDTO imageDTO = new ImageDTO();
                imageDTO.setGoodsId(hospitalId);
                imageDTO.setImageType(GoodsEnum.HOSPITAL.getCode());
                imageDTO.setRows(6);
                imageDTO.setImageState(ImageStateEnum.NORMAL.getCode());
                imageDTO.setSort("id");
                List<ImageModel> imageModelList = imageService.getList(imageDTO);
                if (imageModelList != null && imageModelList.size() > 0) {
                    List<ImageVO> imageVOList = new ArrayList<>();
                    for (ImageModel imagemodel : imageModelList) {
                        ImageVO imageVO = new ImageVO();
                        imageVO.copyPropertis(imagemodel);
                        imageVOList.add(imageVO);
                    }
                    hospitalVO.setShowImage(imageVOList);
                }
                JsonUtils.toSuccessResult(request, response, hospitalVO);
            }
        } catch (Exception e) {
            JsonUtils.toErrorResult(request, response, "参数有误!", OprationStatus.FAIL.getCode());
            e.printStackTrace();
        }

    }
}
